/**
 * @author cqb
 */
/**
 * @requires OpenLayers/SingleFile.js
 */
OpenLayers.Util = OpenLayers.Util || {};
OpenLayers.Util.getRootPath = function(){
	var scripts = document.getElementsByTagName('script');
	var path = "";
	for(var i in scripts){
		var s = scripts[i];
		if (s.src){
			var regexp = new RegExp(/\/OpenLayers(\.min)?\.js(\?.*)?/);
			if (s.src.match(regexp)){
				path = s.src.replace(regexp, '')+"/";
			}
		}
	}
	
	return path;
}

OpenLayers.Util.createUnitCallBack = function(cback){
	var time = new Date().getTime();
	var callback = "OpenLayers.Util.CallBack_"+time;
	OpenLayers.Util["CallBack_"+time] = cback ? cback : function(){
		
	};
	return callback;
}

OpenLayers.Util.JSONP = function(url, cback){
	var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement;
	var script = document.createElement("script");
	script.async = "async";
	script.type = "text/javascript";
	var callback = OpenLayers.Util.createUnitCallBack(cback);
	script.src = url+"&callback="+callback;
	
	script.onload = script.onreadystatechange = function( _, isAbort ) {
		if ( isAbort || !script.readyState || /loaded|complete/.test( script.readyState ) ) {
			// Handle memory leak in IE
			script.onload = script.onreadystatechange = null;

			// Remove the script
			if ( head && script.parentNode ) {
				head.removeChild( script );
			}

			// Dereference the script
			script = undefined;
		}
	}
	head.insertBefore( script, head.firstChild );
}

OpenLayers.Util.getCircleGeometry = function(center, radius, ratial){
	var theta = 360 / ratial;
	var deg = Math.PI / 180;
	var ps = [];
	for(var i=0; i <= ratial; i++){
		var x = center.lon + Math.cos(theta * i * deg) * radius;
		var y = center.lat + Math.sin(theta * i * deg) * radius;
		ps.push(new OpenLayers.Geometry.Point(x, y));
	}
	
	var line = new OpenLayers.Geometry.LinearRing(ps);
	var polygon = new OpenLayers.Geometry.Polygon([line]);
	
	return polygon;
}

OpenLayers.Util.calculateWGS84Area = function(points){
    var totalarea = 0;
    var Count = points.length;
    if(points.length > 3){
        if(points[0].x == points[Count-1].x && points[0].y == points[Count-1].y){
            points.pop();
            Count = points.length;
        }
        var LowX=0.0;
        var LowY=0.0;
        var MiddleX=0.0;
        var MiddleY=0.0;
        var HighX=0.0;
        var HighY=0.0;

        var AM = 0.0;
        var BM = 0.0;
        var CM = 0.0;

        var AL = 0.0;
        var BL = 0.0;
        var CL = 0.0;

        var AH = 0.0;
        var BH = 0.0;
        var CH = 0.0;

        var CoefficientL = 0.0;//Coefficient系数
        var CoefficientH = 0.0;

        var ALtangent = 0.0; //tangent切线
        var BLtangent = 0.0;
        var CLtangent = 0.0;

        var AHtangent = 0.0;
        var BHtangent = 0.0;
        var CHtangent = 0.0;

        var ANormalLine = 0.0; //NormalLine法线
        var BNormalLine = 0.0;
        var CNormalLine = 0.0;

        var OrientationValue = 0.0; //Orientation  Value方向 值

        var AngleCos = 0.0;//余弦角

        var Sum1 = 0.0;
        var Sum2 = 0.0;
        var Count2 = 0;
        var Count1 = 0;


        var Sum = 0.0;
        var Radius = 6378137; //半径

        for(i=0;i<Count;i++) {
            if (i == 0) {
                LowX = points[Count-1].x * Math.PI / 180;//换算成弧度
                LowY = points[Count-1].y * Math.PI / 180;
                MiddleX = points[0].x * Math.PI / 180;
                MiddleY = points[0].y * Math.PI / 180;
                HighX = points[1].x * Math.PI / 180;
                HighY = points[1].y * Math.PI / 180;
            } else if (i == Count - 1) {
                LowX = points[Count-2].x * Math.PI / 180;
                LowY = points[Count-2].y * Math.PI / 180;
                MiddleX = points[Count-1].x * Math.PI / 180;
                MiddleY = points[Count-1].y * Math.PI / 180;
                HighX = points[0].x * Math.PI / 180;
                HighY = points[0].y * Math.PI / 180;
            } else {
                LowX = points[i-1].x * Math.PI / 180;
                LowY = points[i-1].y * Math.PI / 180;
                MiddleX = points[i].x * Math.PI / 180;
                MiddleY = points[i].y * Math.PI / 180;
                HighX = points[i+1].x * Math.PI / 180;
                HighY = points[i+1].y * Math.PI / 180;
            }

            AM = Math.cos(MiddleY) * Math.cos(MiddleX);
            BM = Math.cos(MiddleY) * Math.sin(MiddleX);
            CM = Math.sin(MiddleY);
            AL = Math.cos(LowY) * Math.cos(LowX);
            BL = Math.cos(LowY) * Math.sin(LowX);
            CL = Math.sin(LowY);
            AH = Math.cos(HighY) * Math.cos(HighX);
            BH = Math.cos(HighY) * Math.sin(HighX);
            CH = Math.sin(HighY);

            CoefficientL = (AM * AM + BM * BM + CM * CM) / (AM * AL + BM * BL + CM * CL);
            CoefficientH = (AM * AM + BM * BM + CM * CM) / (AM * AH + BM * BH + CM * CH);

            ALtangent = CoefficientL * AL - AM;
            BLtangent = CoefficientL * BL - BM;
            CLtangent = CoefficientL * CL - CM;
            AHtangent = CoefficientH * AH - AM;
            BHtangent = CoefficientH * BH - BM;
            CHtangent = CoefficientH * CH - CM;


            AngleCos = (AHtangent * ALtangent + BHtangent * BLtangent + CHtangent * CLtangent) / (Math.sqrt(AHtangent * AHtangent + BHtangent * BHtangent + CHtangent * CHtangent) * Math.sqrt(ALtangent * ALtangent + BLtangent * BLtangent + CLtangent * CLtangent));

            AngleCos = Math.acos(AngleCos);

            ANormalLine = BHtangent * CLtangent - CHtangent * BLtangent;
            BNormalLine = 0 - (AHtangent * CLtangent - CHtangent * ALtangent);
            CNormalLine = AHtangent * BLtangent - BHtangent * ALtangent;

            if (AM != 0)
                OrientationValue = ANormalLine / AM;
            else if (BM != 0)
                OrientationValue = BNormalLine / BM;
            else
                OrientationValue = CNormalLine / CM;

            if (OrientationValue > 0) {
                Sum1 += AngleCos;
                Count1++;

            } else {
                Sum2 += AngleCos;
                Count2++;
                //Sum +=2*Math.PI-AngleCos;
            }
        }

        if(Sum1>Sum2){
            Sum = Sum1+(2*Math.PI*Count2-Sum2);
        }
        else{
            Sum = (2*Math.PI*Count1-Sum1)+Sum2;
        }

        totalarea = (Sum-(Count-2)*Math.PI)*Radius*Radius;
    }
    
    return totalarea;
}

/**
 * 单polygon不包含有hole的polygon
 */
OpenLayers.Util.calculateWGS84AreaFromGeometry = function(geometry){
    if(geometry instanceof OpenLayers.Geometry.Polygon){
        var line = geometry.components[0];
        var points = line.components;
        var area = OpenLayers.Util.calculateWGS84Area(points);
        return area;
    }
    
    return 0;
}